package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.util.Assert;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@RestController
@Api(tags = "借款后台管理系统")
@RequestMapping("/api/core/borrower")
public class ApiBorrowerController {
    @Resource
    private BorrowerService borrowerService;
    @PostMapping("/save")
    @ApiOperation("借款人资料保存")
    public R save(@RequestBody BorrowerVO borrowerVO,HttpServletRequest request){
        String token = request.getHeader("token");

        if(!StringUtils.isEmpty(token)) {
            String userId = JwtUtils.getUserId(token);
            borrowerService.saveBorrower(borrowerVO,userId);


        }
        Assert.notNull(token,ResponseEnum.LOGIN_AUTH_ERROR);
        return R.ok();
    }
    @GetMapping("/getInitStatus")
    public R getInitStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = null;
        if(!StringUtils.isEmpty(token)) {
            userId = JwtUtils.getUserId(token);

        }
        Integer borrowerStatus = borrowerService.getInitStatus(userId);
        return R.ok().data("borrowerStatus",borrowerStatus);
    }
}

